package org.example.ttms.controller;

import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.scene.layout.StackPane;
import javafx.util.Callback;
import org.example.ttms.customize.Detail;
import org.example.ttms.customize.Pop_ups;
import org.example.ttms.entityClass.train.Route;

import java.util.HashMap;

public class RouteTableViewController {

    @FXML
    private StackPane stackPane;

    @FXML
    private TableView<Route> tableView;

    @FXML
    private TableColumn<Route, Integer> idTableColumn;

    @FXML
    private TableColumn<Route, String> startStationTableColumn;

    @FXML
    private TableColumn<Route, String> endStationTableColumn;

    @FXML
    private TableColumn<Route, Integer> stationNumTableColumn;

    @FXML
    private TableColumn<Route, Integer> tripsNumTableColumn;

    @FXML
    private TableColumn<Route, Void> detailTableColumn;

    @FXML
    private TableColumn<Route, Void> delTableColumn;

    @FXML
    private Label label;

    public void initialize(){
        Route route = new Route();
        HashMap<Integer, Route> hashMap = route.obtainAllRoute();
        ObservableList<Route> observableList = FXCollections.observableArrayList(hashMap.values());

        idTableColumn.setCellValueFactory(cellData -> cellData.getValue().routeIdProperty().asObject());
        startStationTableColumn.setCellValueFactory(cellData -> cellData.getValue().startStationProperty());
        endStationTableColumn.setCellValueFactory(cellData -> cellData.getValue().endStationProperty());
        stationNumTableColumn.setCellValueFactory(cellData -> cellData.getValue().stationNumProperty().asObject());
        tripsNumTableColumn.setCellValueFactory(cellData -> cellData.getValue().tripsNumProperty().asObject());
        detailTableColumn.setCellFactory(new Callback<TableColumn<Route, Void>, TableCell<Route, Void>>() {
            @Override
            public TableCell<Route, Void> call(TableColumn<Route, Void> routeVoidTableColumn) {
                return new TableCell<Route, Void>(){
                    private final Button detailButton = new Button("查看详情");
                    {
                        detailButton.setOnAction(e -> {
                            Detail detail = new Detail();
                            detail.routeDetailWindow(getTableView().getItems().get(getIndex()));
                        });
                    }
                    @Override
                    public void updateItem(Void item, boolean empty){
                        super.updateItem(item, empty);
                        if(empty){
                            setGraphic(null);
                        }
                        else{
                            setGraphic(detailButton);
                        }
                    }
                };
            }
        });
        delTableColumn.setCellFactory(new Callback<TableColumn<Route, Void>, TableCell<Route, Void>>() {
            @Override
            public TableCell<Route, Void> call(TableColumn<Route, Void> routeVoidTableColumn) {
                return new TableCell<Route, Void>(){
                  private final Button delButton = new Button("  删除  ");
                    {
                        delButton.setOnAction(e -> {
                            Route route1 = getTableView().getItems().get(getIndex());
                            if(judgeDelRoute(route1)){
                                observableList.remove(route1);
                                route.storageRoute(observableList);
                                route.decreaseRouteNum(route1);
                            }
                        });
                    }
                    @Override
                    public void updateItem(Void item, boolean empty){
                        super.updateItem(item, empty);
                        if(empty){
                            setGraphic(null);
                        }
                        else{
                            setGraphic(delButton);
                        }
                    }
                };
            }
        });

        label.setVisible(observableList.isEmpty());
        observableList.addListener((ListChangeListener.Change<? extends Route> c) ->{
            label.setVisible(observableList.isEmpty());
        } );
        tableView.setItems(observableList);
    }
    //判断是否可删除
    public boolean judgeDelRoute(Route route){
        Pop_ups popUps = new Pop_ups();
        if(route.getTripsNum() == 0){
            popUps.pop_ups4("路线删除成功");
            return true;
        }
        else{
            popUps.pop_ups3("该路线存在车次,无法删除");
            return false;
        }
    }

}
